﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zen4SyncPOCO.Enum.Admin;

namespace Zen4SyncClient
{
    internal static class SqlValueParser
    {
        internal static object Parse(byte[] value, SqlValueTypeEnum valueType)
        {
            return Parse(Encoding.UTF8.GetString(value), valueType);
        }

        internal static object Parse(object value, SqlValueTypeEnum valueType)
        {
            return Parse(value.ToString(), valueType);
        }

        private static object Parse(string value, SqlValueTypeEnum sqlValueType)
        {
            switch (sqlValueType)
            {
                case SqlValueTypeEnum.UniqueIdentifier:
                    return new Guid(value);
                case SqlValueTypeEnum.Int:
                    return Int32.Parse(value);
                case SqlValueTypeEnum.Varchar:
                    return value;
                case SqlValueTypeEnum.Nvarchar:
                    return value;
                case SqlValueTypeEnum.Bit:
                    return Boolean.Parse(value);
                case SqlValueTypeEnum.DateTime:
                    return DateTime.Parse(value);
                case SqlValueTypeEnum.Xml:
                    return value;
                case SqlValueTypeEnum.SmallInt:
                    return Int16.Parse(value);
                default:
                    throw new Exception(String.Format("Unknown Zen4Sync SQL Type : {0}", Enum.GetName(typeof(SqlValueTypeEnum), sqlValueType)));
            }
        }
    }
}
